/*********************************************************************************
 *     Copyright:  (C) 2022 Nikel
 *                 All rights reserved.
 *
 *    Filename:  at_cmd.h
 *    Description:  This file is for AT command module
 *
 *    Version:  1.0.0
 *    Author:  zhouyanlin <www.zhouyanlin1222@qq.com>
 *    ChangeLog:  1, Release initial version on "06/04/22 22:52:15"
 *
 ********************************************************************************/

#ifndef INC_AT_CMD_H_
#define INC_AT_CMD_H_

#include "usart.h"

/*
 * Description:
 * The following variables, functions, and macro definitions
 * have been defined in the USART layer to implement the code layer.
 * To use this file, the following functions need to be implemented
 * in the USART layer as a prerequisite. USART initialization,
 * USART sending data, USART receiving data.
 *
 * */
#if 0

#define	at_uart			  &huart3		     	/* at_cmd Serial port in use */
extern char               g_atcmd_rxbuf[256];	/* The received buffer of the AT command module */
extern uint8_t            g_atcmd_bytes;	 	/* The size of data received by the AT command module */

/*Clears macro that received the buffer */
#define clear_atcmd_rxbuf() do { memset(g_atcmd_rxbuf, 0, sizeof(g_atcmd_rxbuf)); \
									g_atcmd_bytes=0; } while(0)

/* USART3 init function */
void MX_USART3_UART_Init(void)
{
  huart3.Instance = USART3;
  huart3.Init.BaudRate = 9600;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }

  /* USART receive messages function */
  HAL_UART_Receive_IT(&huart3 , &s_atcmd_rxch, 1);
}

/* USART send messages function */
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout);

#endif


/*
 * Description:
 * 		Send an AT command to the module and determine whether the expected string is received within the timeout period.
 * 		If a BUf is passed in, the received AT command response data is returned through outbuf.
 *
 * Input Value:
 * 		@atcmd: The AT command to send
 * 		@expect_reply: The expectant reply if not specified, defaults to waiting for "\r\nOK\r\n" to pass NULL
 * 		@timeout: time-out period
 * 		@outbuf: Buff to load the return value
 * 		@size_outbuf: The outbuf size
 *
 * Return Value: -1: The input parameter is incorrect;
 * 				 0: the AT command receives the expected string;
 * 				 -2: The AT command replies to timeout;
 * 				 -3: A string such as ERROR is received.
 *
 */
extern int send_atcmd(char *atcmd, char *expect_reply, unsigned int timeout, char *outbuf, int size_outbuf);


/*
 * Description:
 * 		Send AT command which will only reply by "OK" or "ERROR", such as AT:
 * 		Reply:   \r\nOK\r\n
 *
 * Input Value:
 * 		@atcmd: The AT command to send
 * 		@timeout: time-out period
 *
 * Return Value: -X: The input parameter is incorrect or other error,
 * 					 reference send_atcmd()function;
 * 				 0: the AT command receives the expected string ok.
 *
 */
extern int send_atcmd_check_ok(char *atcmd, unsigned int timeout);


/*
 * Description:
 * 		Send AT command which will reply by a value directly in a single line, such as AT+CGMM:
 * 		Reply:   \r\nEC20F\r\nOK\r\n
 *
 * Input Value:
 * 		@atcmd: The AT command to send
 * 		@timeout: time-out period
 * 		@outbuf: Buff to load the return value
 * 		@size_outbuf: The outbuf size
 *
 * Return Value: -X: The input parameter is incorrect or other error,
 * 					 reference send_atcmd()function;
 * 				 0:  the AT command receives the string .
 *
 */
extern int send_atcmd_check_value(char *atcmd, unsigned int timeout, char *outbuf, int size_outbuf);


/*
 * Description:
 * 		Send AT command which will reply by the value  with a prefix "+CMD: " line, such as AT+CSQ:
 * 		Reply:   \r\n+CSQ: 26,99\r\nOK\r\n
 *
 * Input Value:
 * 		@atcmd: The AT command to send
 * 		@timeout: time-out period
 * 		@outbuf: Buff to load the return value
 * 		@size_outbuf: The outbuf size
 *
 * Return Value: -X: The input parameter is incorrect or other error,
 * 					 reference send_atcmd()function;
 * 				 0:  the AT command receives the string .
 *
 */
extern int send_atcmd_check_request(char *atcmd, unsigned int timeout, char *outbuf, int size_outbuf);


/*
 * Description:
 * 		Send the "AT" command to the module to confirm whether the module responds normally
 *
 * Input Value:
 * 		@times: The number of attempts to send the AT command test
 *
 * Return Value: -X: The module does not work properly
 * 				 0:  The module recovers normally.
 *
 */
extern int send_atcmd_check_module(void);

#endif /* INC_AT_CMD_H_ */
